package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.Personnel;
import com.globallogic.javatrainings.sergiiivanov.model.PersonnelManager;
import com.globallogic.javatrainings.sergiiivanov.model.Room;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

/**
 * <pre>
 * 50  7.    Personnel
 * 51  7.1.  Personnel has name, email, phone
 * 52  7.1.1.    all could be changed
 * 53  7.1.2.    name should be unique within students of one university
 * 54  7.2.  Personnel has resume
 * 55  7.2.1.    records could be added and deleted
 * 56  7.3.  Personnel could be assigned to room
 * 57  7.3.1.    any number of rooms
 * 58  7.3.2.    could be unassigned at any time
 * 59  7.4.  Personnel list should be available
 * 60  7.5.  Assigned rooms list should be available
 * </pre>
 * 
 * @author Sergey
 * 
 */
public class PersonnelTest extends BasicPersonTest {
    private static final String RESPONCIBILITIES = "responcibilities";

    Personnel personnel = new Personnel();

    /**
     * <pre>
     * 50  7.    Personnel
     * 51  7.1.  Personnel has name, email, phone
     * 52  7.1.1.    all could be changed
     * ...
     * 54  7.2.  Personnel has resume
     * 55  7.2.1.    records could be added and deleted
     * </pre>
     */
    public PersonnelTest() throws Exception {
        super.person = this.personnel;
        super.BasicPersonSetup();
    }

    @Test
    public void setGetResponcibilitiesTest() {
        personnel.setResponcibilities(RESPONCIBILITIES);
        assertEquals(RESPONCIBILITIES, personnel.getResponcibilities());

        personnel.setResponcibilities(null);
        assertEquals(empty, personnel.getResponcibilities());

        personnel.setResponcibilities(empty);
        assertEquals(empty, personnel.getResponcibilities());
    }

    /**
     * <pre>
     * 56  7.3.  Personnel could be assigned to room
     * 57  7.3.1.    any number of rooms
     * 58  7.3.2.    could be unassigned at any time
     * ...
     * 60  7.5.  Assigned rooms list should be available
     * </pre>
     * 
     * @throws Exception
     */
    @Test
    public void assignRoomTest() throws Exception {
        assertArrayEquals(new Room[] {}, personnel.getAssignedRooms().toArray());
        Room r1 = newRoom(rname), r2 = newRoom(rname2);
        r1.setResponsiblePerson(personnel);
        assertArrayEquals(new Room[] { r1 }, personnel.getAssignedRooms().toArray());
        r2.setResponsiblePerson(personnel);
        assertArrayEquals(new Room[] { r1, r2 }, personnel.getAssignedRooms().toArray());
        assertEquals(personnel, r1.getResponsiblePerson());
        assertEquals(personnel, r2.getResponsiblePerson());
        Personnel p = new Personnel(nPerson2, mobile, email);
        r2.setResponsiblePerson(p);
        assertEquals(personnel, r1.getResponsiblePerson());
        assertNotSame(personnel, r2.getResponsiblePerson());
    }

    private Room newRoom(String newRoomName) throws UniversityWarning {
        Room r = new Room();
        r.setCapacity(0);
        r.setName(newRoomName);
        r.setToAuditorium();
        return r;
    }

    /**
     * <pre>
     * 53  7.1.2.    name should be unique within students of one university 
     * </pre>
     * @throws Exception
     */
    @Test
    public void nameUniqueTest() throws Exception {
        PersonnelManager pm = u.personnel;
        pm.addPersonnel(name, mobile, email);
        try {
            pm.addPersonnel(name, mobile, email);
            fail("MISSED "+ ErrorCodes.PERSON_NAME_NOT_UNIQUE);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NOT_UNIQUE+ name, e.getMessage());
        }
    }
    
    @Test
    public void nameValidViaManagerTest() {
        PersonnelManager pm = u.personnel;
        try {
            pm.addPersonnel(null, mobile, email);
            fail("MISSED Person fields validation");
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NULL, e.getMessage());
        }
        try {
            pm.addPersonnel(null, null, null);
            fail("MISSED Person fields validation");
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NULL, e.getMessage());
        }
        try {
            pm.addPersonnel(empty, mobile, email);
            fail("MISSED Person fields validation");
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NULL, e.getMessage());
        }
    }
    
    /**
     * <pre>
     * 59  7.4.  Personnel list should be available
     * </pre>
     * @throws Exception
     */
    @Test
    public void personnelListTest() throws Exception {
        PersonnelManager pm = u.personnel;
        pm.addPersonnel(nPerson1, mobile, email);
        pm.addPersonnel(nPerson2, mobile, email);
        assertArrayEquals(new String[]{nPerson1, nPerson2}, pm.getPersonnelList());
    }
    
    @Test
    public void managerGetByIndexTest() throws Exception {
        PersonnelManager pm = u.personnel;
        try {
            pm.getPersonnelByIndex(0);
            fail("Missed " + ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+0);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+0, e.getMessage());
        }
        pm.addPersonnel(nPerson1, mobile, email);
        pm.addPersonnel(nPerson2, mobile, email);
        try {
            pm.getPersonnelByIndex(2);
            fail("Missed " + ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+2);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+2, e.getMessage());
        }
        assertEquals(nPerson1, pm.getPersonnelByIndex(0).getName());
        assertEquals(nPerson2, pm.getPersonnelByIndex(1).getName());
    }
    @Test
    public void managerRemoveTest() throws Exception {
        PersonnelManager pm = u.personnel;
        try {
            pm.removePerson(pm.getPersonnelByIndex(0));
            fail("Missed " + ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+0);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSONNEL_NOT_FOUND_BY_INDEX+0, e.getMessage());
        }
        pm.addPersonnel(nPerson1, mobile, email);
        pm.addPersonnel(nPerson2, mobile, email);
        Personnel p = pm.getPersonnelByIndex(1);
        pm.removePerson(p);
        pm.removePerson(pm.getPersonnelByIndex(0));
        try {
            pm.removePerson(p);
            fail("Missed " + ErrorCodes.PERSONNEL_NOT_FOUND);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSONNEL_NOT_FOUND, e.getMessage());
        }
    }

}
